package net.chenlin.dp.modules.newBusiness.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.modules.mqtt.SubscribeSample;
import net.chenlin.dp.modules.newBusiness.service.DeviceMessageService;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Yansgm
 * @Description:
 * @Date: 2025-04-14 21:26:41
 */
@Slf4j
@RestController
@RequestMapping("/device")
@Api(tags = "ysm-新版-监测设备管理")
public class DeviceMessageController {

    @Autowired
    private DeviceMessageService service;

    @Autowired
    private SubscribeSample sample;

    @RequestMapping(value = "/insertDeviceMessage", method = RequestMethod.GET)
    @ApiOperation(value = "数据保存")
    public R insertDeviceMessage(String msg) {
        return service.insertDeviceMessage(msg);
    }

    @RequestMapping(value = "/send/query", method = RequestMethod.GET)
    @ApiOperation(value = "下发查询命令")
    public R sendQuery(String deviceNo) {

        String replyClient = deviceNo+"reply_client";
        String replyTopic = "monitor/"+deviceNo+"/query_reply";
        sample.start(replyClient,replyTopic);

        String broker = "tcp://1.94.111.92:1883";
        String topic = "monitor/"+deviceNo+"/query";
        String username = "root";
        String password = "Owendeng123";
        String clientid = deviceNo+"/publish_client";
        String content = "{\n" +
                "    \"cmd\": \"all\",\n" +
                "    \"timestamp\": \"2024-10-19T14:55:00Z\"\n" +
                "}";
        int qos = 0;

        try {
            MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
            // 连接参数
            MqttConnectOptions options = new MqttConnectOptions();
            // 设置用户名和密码
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setConnectionTimeout(60);
            options.setKeepAliveInterval(60);
            // 连接
            client.connect(options);
            // 创建消息并设置 QoS
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            // 发布消息
            client.publish(topic, message);
            System.out.println("下发查询命令 == Message published");
            System.out.println("下发查询命令 == topic: " + topic);
            System.out.println("下发查询命令 == message content: " + content);
            // 关闭连接
            client.disconnect();
            // 关闭客户端
            client.close();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
        return R.ok("查询指令发送成功！");
    }

    @RequestMapping(value = "/getQueryData", method = RequestMethod.GET)
    @ApiOperation(value = "获取查询的回复报文")
    public R getQueryData(String deviceNo) {
        return service.getQueryData(deviceNo);
    }
}
